package com.ssis.core;

import java.util.HashMap;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.util.Log;

import com.ssis.core.Cell.CellType;
import com.ssis.core.Game.state_types;
import com.ssis.graphics.Dialog;

public class InputHandler {
	private Game game;
	private HashMap<Integer,CellType> buttons;

	public InputHandler(Game game){
		this.game = game;
		buttons = new HashMap<Integer,CellType>();
		buttons.put(1, CellType.OFFICE);
		buttons.put(2, CellType.DORM);
		buttons.put(3, CellType.GENERATOR);
		buttons.put(4, CellType.CAPACITOR);
	}
	
	public void inputClick(int button, int x, int y, boolean held) {
		if(game.state==Game.state_types.MAIN_MENU) {
			game.state=Game.state_types.STATION;
		}
		else if (game.state==Game.state_types.STATION){
			if (x < 672) { // Clicked in play area
				if (button == 0) {
					game.station().moveCursor(x, y);
				} else if (button == 1) {
					game.station().move(Mouse.getDX(), -Mouse.getDY());
				} else if (button == 2) {
					game.station().resetPosition();
				}
			} else if (held == false){  // Clicked on HUD
				int hudbutton = game.hudStation().click(x, y);
				if (y < HUDStation.BUTTON_WIDTH * 4 && hudbutton > 0 && game.hudStation().getButtonType(hudbutton-1) != null) { // Clicked on a build cell button
					Log.debug("Button " + hudbutton);
					game.station().addCell(game.hudStation().getButtonType(hudbutton-1));
				} else { // Clicked on a different button
					if(hudbutton == 6) {
						game.state = Game.state_types.DIALOG;
						Dialog.setConversation(game.rand().nextInt(11));
					}
					if(hudbutton == 7) {
						game.station().cycle();
					}
					if(hudbutton == 8) {
						game.state = state_types.CREW_MENU;
					}
				}
			}
		}
		else if (game.state==Game.state_types.DIALOG && held == false) {
			if (! Dialog.next())
				game.state = Game.state_types.STATION;
		}
		else if (game.state==Game.state_types.CREW_MENU && held == false) {
			int hudbutton = game.hudCrew().click(x, y);
			Log.debug("Clicked CREW button" + hudbutton);
			if (hudbutton == 1) {
				if (game.station().crewSize() < game.station().housingCapacity()  && game.station().cargo().getCredits() > 10) {
					game.station().addCrew(new Crew());
					game.station().cargo().changeCredits(-10);
				}
			} else if (hudbutton == 2){
				game.station().removeCrew(game.hudCrew().selectedCrew());
			} else if (hudbutton == 3){
				game.state = Game.state_types.STATION;
			} else if (hudbutton == 4){
				game.hudCrew().selectCrew((y-HUDCrew.LIST_Y_OFFSET)/HUDCrew.LINE_WIDTH);
			} else if (hudbutton == 5){
				game.hudCrew().selectAssignmentType((x-HUDCrew.ASSIGNMENTS_X_OFFSET)/HUDCrew.ASSIGNMENT_WIDTH + (y-HUDCrew.ASSIGNMENTS_Y_OFFSET)/HUDCrew.ASSIGNMENT_WIDTH * 4);
				game.hudCrew().getAssignmentChoices();
			} else if (hudbutton == 6){
				game.hudCrew().selectAssignment((y-HUDCrew.LIST_Y_OFFSET)/HUDCrew.LINE_WIDTH);
			}
		}
	}
}
